package cn.edu.gues.infosystam.controller;

import cn.edu.gues.infosystam.entity.Student;
import cn.edu.gues.infosystam.entity.Teacher;
import cn.edu.gues.infosystam.entity.Template;
import cn.edu.gues.infosystam.entity.User;
import cn.edu.gues.infosystam.service.StudentService;
import cn.edu.gues.infosystam.service.TeacherService;
import cn.edu.gues.infosystam.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/")
public class LoginController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StudentService studentService;

    @RequestMapping("Login")
    public String login(){
        return "Login";
    }

    @RequestMapping("Home")
    public String home(){

        return "Home";
    }

    /**
     * 用户退出（直接删除登录信息并跳转至登录界面）
     * @param httpSession
     * @return
     */
    @RequestMapping("Logout")
    public String logout(HttpSession httpSession){
        httpSession.removeAttribute("userInfo");
        return "Login";
    }

    @ResponseBody
    @PostMapping("RePasswd")
    public R editPassword(String no, Integer roles, String passwd, HttpSession httpSession){
        boolean update = false;
        if (roles == 2){
            Student student = new Student();
            student.setSpassword(passwd);
            QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>();
            queryWrapper.eq("sno", no);
            update = studentService.update(student, queryWrapper);
        }else {
            Teacher teacher = new Teacher();
            teacher.setTpassword(passwd);
            QueryWrapper<Teacher> queryWrapper = new QueryWrapper<Teacher>();
            queryWrapper.eq("tno", no);
            update = teacherService.update(teacher, queryWrapper);
        }

        if (update){
            User userInfo = (User) httpSession.getAttribute("userInfo");
            userInfo.setPassword(passwd);

            httpSession.setAttribute("userInfo", userInfo);
            return R.success();
        } else return R.error();
    }

    /**
     * 异步登录请求
     * @param username 用户名
     * @param password 密码
     * @param roles 权限
     * @param httpSession
     * @return
     */
    @PostMapping("Signin")
    @ResponseBody
    public R Signin(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("roles") Integer roles, HttpSession httpSession){

//        User user = new User();
        User user = User.getUser();
        user.setAccount(username);
        user.setPassword(password);
        user.setRoles(roles);

        System.out.println("账号："+username);
        System.out.println("密码："+password);
        System.out.println("权限："+roles);
        if (roles != 2){
            QueryWrapper<Teacher> queryWrapper = new QueryWrapper<Teacher>();
            queryWrapper.eq("roles", roles)
                    .eq("tpassword", password)
                    .and(i->i.eq("tno",username).or().eq("tphone", username));

            Teacher teacher = teacherService.getOne(queryWrapper);
            if (teacher != null) {
                user.setUserName(teacher.getTname());
                user.setUserInfo(teacher);
            }
        } else{
            QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>();
            queryWrapper.eq("sno", username)
                    .eq("spassword", password);
            Student student = studentService.getOne(queryWrapper);
            if (student != null) {
                user.setUserName(student.getSname());
                user.setUserInfo(student);
            }
        }

        if (user.getUserInfo() != null) {
            httpSession.setAttribute("userInfo", user);
            httpSession.setAttribute("homeHref", roles!=2?"/Home":"/Report/List");

            // 返回用户对象
            return R.success().data("userInfo", user.getUserInfo())
                    // 如果登录的时学生则跳转至待填报界面
                    .data("href", roles!=2?"/Home":"/Report/List");
        }

        return R.error().message("你的账号或密码不正确！！！");
    }

}
